package com.common.util;

import java.util.Random;


public class GuidUtil
{

 private GuidUtil()
 {
     lastTime = 0L;
     lastIndex = 0L;
 }

 public static GuidUtil getInstance()
 {
     if(instance == null)
    	 instance = new GuidUtil();
     return instance;
 }

 public String getNewId()
 {
     long currTime = System.currentTimeMillis();
     String timestampStr = Long.toString(currTime, 32);
     StringBuilder idStr = new StringBuilder(timestampStr);
     if(timestampStr.length() != 9)
         fixString(idStr, 9);
     String storeSystemKey = getStoreSystemKey();
     idStr.append(storeSystemKey);
     if(currTime != lastTime)
     {
         lastTime = currTime;
         lastIndex = 0L;
     } else
     {
         lastIndex++;
     }
     StringBuilder storeIndex = new StringBuilder();
     storeIndex.append(Long.toString(lastIndex, 32));
     fixString(storeIndex, 4);
     idStr.append(storeIndex.toString());
     return idStr.toString();
 }

 private String getStoreSystemKey()
 {
     if(systemKey != null)
     {
         try
         {
             if(systemKey != null)
             {
                 long maxKeyValue = 1L;
                 int power = (int)Math.sqrt(32D);
                 for(int i = 0; i < 3; i++)
                     maxKeyValue <<= power;

                 Random random = new Random();
                 int keyValue = random.nextInt((int)maxKeyValue);
                 systemKey = Integer.toString(keyValue, 32);
                 StringBuilder sb = new StringBuilder();
                 sb.append(systemKey);
                 fixString(sb, 3);
                 systemKey = sb.toString();
             }
         }
         catch(Exception e)
         {
             e.printStackTrace();
             StringBuilder errorSystemKey = new StringBuilder();
             fixString(errorSystemKey, 3);
             systemKey = errorSystemKey.toString();
         }
     }
     return systemKey;
 }

 private static void fixString(StringBuilder sb, int strLength)
 {
     if(sb != null && strLength > 0)
     {
         String oldStr = sb.toString();
         int fixLength = strLength - sb.length();
         sb.setLength(0);
         if(fixLength < 0)
         {
             sb.append(oldStr.substring(-fixLength));
         } else
         {
             for(int i = 0; i < fixLength; i++)
                 sb.append('0');

             sb.append(oldStr);
         }
     }
 }

 public static String[] parseId(String id)
 {
     String result[] = null;
     if( id != null && id.length() == 16)
     {
         result = new String[3];
         int split1 = 9;
         int split2 = 12;
         String timestampStr = id.substring(0, split1);
         result[0] = String.valueOf(Long.parseLong(timestampStr, 32));
         String systemKeyStr = id.substring(split1, split2);
         result[1] = String.valueOf(Long.parseLong(systemKeyStr, 32));
         String indexStr = id.substring(split2);
         result[2] = String.valueOf(Long.parseLong(indexStr, 32));
     }
     return result;
 }

 private volatile long lastTime;
 private volatile long lastIndex;
 private static String systemKey = "";
 private static GuidUtil instance;

}
